<?php
/**
 * twig 模板支持服务。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @version   v1.0
 */

namespace Lib\Twig;

use Illuminate\Support\ServiceProvider;

class TwigViewServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->registerLoader();
        $this->registerTwig();

        $this->app->bind('view', function ($app) {
            return new TwigFactory($app);
        });
    }
    
    public function registerLoader()
    {
        $this->app->singleton('twig.loader', function ($app) {
            $view_paths = $app['config']['view.paths'];
            $loader = new \Twig_Loader_Filesystem($view_paths);

            return $loader;
        });
    }
    
    public function registerTwig()
    {
        $this->app->singleton('twig', function ($app) {
            $options = [
                'debug' => $app['config']['app.debug'],
                'cache' => $app['config']['view.compiled'],
            ];

            $twig = new \Twig_Environment($app['twig.loader'], $options);
            // register Twig Extensions
            $twig->addExtension(new \Twig_Extension_Debug());
            // register Twig globals
            $twig->addGlobal('app', $app);
            
            return $twig;
        });
    }
}
